<template>
  <div id="webAccountIndex">
    <el-row>
      <!-- 表格查询条件 -->
      <el-col :span="24">
        <el-form :inline="true" ref="stationForm" :model="queryForm">
          <el-form-item>
            <el-input v-model="queryForm.accountCode" placeholder="账号名"></el-input>
          </el-form-item>
          <el-form-item>
            <el-input v-model="queryForm.accountEmail" placeholder="账号注册邮箱"></el-input>
          </el-form-item>
          <el-form-item>
            <el-input v-model="queryForm.accountCountry" placeholder="账号注册国家"></el-input>
          </el-form-item>
          <el-form-item>
            <el-input v-model="queryForm.accountPhoneNumber" placeholder="账号注册手机号"></el-input>
          </el-form-item>
          <el-form-item>
            <el-input v-model="queryForm.webStaffName" placeholder="使用者客户姓名"></el-input>
          </el-form-item>
          <el-form-item>
            <gd_param type="select"
                      paramCode="webAccountState"
                      placeholder="账号状态"
                      v-model="queryForm.webAccountCode" />
          </el-form-item>
          <el-form-item>
            <gd_param type="select"
                      paramCode="webAccountType"
                      placeholder="账号类型"
                      v-model="queryForm.webAccountTypeCode" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="getTableDatas()">查询</el-button>
            <el-button @click="resetQueryForm" icon="el-icon-refresh-right">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <el-row>
      <!-- 数据表格 -->
      <el-col :span="24">
        <el-row>
          <el-col :span="24">
            <el-button type="primary" icon="el-icon-plus" @click="dialog.saveOrEditDiv=true">新增账号</el-button>
            <el-popconfirm title="这是一段内容确定删除吗？" @confirm="moreDeletes">
              <el-button slot="reference" type="danger" icon="el-icon-delete">批量删除账号</el-button>
            </el-popconfirm>
            <el-popconfirm title="此操作可能需要花费一定时间，确定要操作吗？" @confirm="moreUpdateGPTLess">
              <el-button slot="reference" type="warning" icon="el-icon-s-check">全部账号更新openai额度</el-button>
            </el-popconfirm>
            <el-upload style="float: left;margin-right: 5px"
              class="avatar-uploader"
              :action="apipath+'/gdones.gpt/webAccount/uploadToAdd'"
              :show-file-list="false">
              <el-button type="warning" icon="el-icon-upload" >数据导入添加账号</el-button>
            </el-upload>
          </el-col>
        </el-row>
        <el-row style="margin-top:10px;">
          <el-col :span="24">
            <el-table :data="tableData" border style="width: 100%" @selection-change="tableDataSelected">
              <el-table-column type="selection" width="55"></el-table-column>
              <el-table-column prop="accountCode" label="账号"></el-table-column>
              <el-table-column prop="accountPwd" label="密码">
                <template slot-scope="scope">
                  <el-input v-model="scope.row.accountPwd" :readonly="true"></el-input>
                </template>
              </el-table-column>
              <el-table-column prop="webAccountTypeName" label="账号类型"  width="150">
                <template slot-scope="scope">
                  <el-tag :type="getTagType(scope.row.webAccountTypeCode)">{{scope.row.webAccountTypeName}}</el-tag>
                </template>
              </el-table-column>
              <el-table-column prop="webAccountName" label="账号状态" width="110">
                <template slot-scope="scope">
                  <el-tag :type="getTagType(scope.row.webAccountCode)">{{scope.row.webAccountName}}</el-tag>
                </template>
              </el-table-column>
              <el-table-column label="租赁状态">
                <template slot-scope="scope">
                  <el-tag v-if="scope.row.leaseEndTime!=null" type="warning">到期时间：{{ scope.row.leaseEndTime | timeFormat }}</el-tag>
                  <el-tag v-else type="danger">未租赁</el-tag>
                </template>
              </el-table-column>
              <el-table-column label="可用额度">
                <template slot-scope="scope">
                  <el-tag v-if="scope.row.gptLimit!=null" type="warning">{{ scope.row.gptLimitLess }}</el-tag>
                  <el-tag v-else type="danger">暂未更新</el-tag>
                </template>
              </el-table-column>
              <el-table-column fixed="right" label="操作" align="center" width="180">
                <template slot-scope="scope">
                  <el-button @click="rowShow(scope.row)" size="small" icon="el-icon-search" circle title="查看"></el-button>
                  <el-button @click="rowEdit(scope.row)" type="primary" size="small" icon="el-icon-edit" circle title="编辑"></el-button>
                  <el-button @click="rowDelete(scope.row)" type="danger" icon="el-icon-delete" size="small" circle title="删除"></el-button>
                  <el-dropdown>
                    <span class="el-dropdown-link"><i class="el-icon-arrow-down el-icon--right"></i></span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item><span @click="rowChangePwd(scope.row)">密码修改</span></el-dropdown-item>
                      <el-dropdown-item><span @click="rowLess(scope.row)">账号租借</span></el-dropdown-item>
                      <el-dropdown-item><span @click="rowLife(scope.row)">账号记录</span></el-dropdown-item>
                      <el-dropdown-item><span @click="rowSaleAccount(scope.row)">售出账号</span></el-dropdown-item>
                      <el-dropdown-item v-if="scope.row.webAccountTypeCode=='1'"><span @click="rowGPTLimit(scope.row)">查询GPT额度</span></el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </template>
              </el-table-column>
            </el-table>
            <el-pagination
              @size-change="tableSizeChange"
              @current-change="tableCurrentChange"
              :current-page="pageData.page"
              :page-sizes="[10, 20, 40, 100]"
              :page-size="pageData.size"
              layout="total, sizes, prev, pager, next, jumper"
              :total="pageData.total"
            ></el-pagination>
          </el-col>
        </el-row>
      </el-col>
    </el-row>

    <!-- 新增\修改用户信息弹窗 -->
    <el-dialog
      :title="(dialog.isEdit == true ? '编辑' : '新增') + '账号'"
      :visible.sync="dialog.saveOrEditDiv"
      :close-on-click-modal="false"
      :show-close="false"
      width="70%"
      :destroy-on-close="true"
    >
      <el-form
        ref="saveOrEditDataForm"
        :rules="saveOrEditDataFormRules"
        :model="saveOrEditData"
        label-width="90px"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="账号名称" prop="accountCode">
              <el-input
                v-model="saveOrEditData.accountCode"
                placeholder="请填写账号名称(一般是邮箱)"
                maxlength="80"
                show-word-limit
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="密码" prop="accountPwd">
              <el-input
                v-model="saveOrEditData.accountPwd"
                placeholder="请填写账号密码"
                maxlength="80"
                show-word-limit
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="注册邮箱" prop="accountEmail">
              <el-input
                v-model="saveOrEditData.accountEmail"
                placeholder="请填写账号注册邮箱"
                maxlength="80"
                show-word-limit
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮箱密码" prop="accountEmailPwd">
              <el-input
                v-model="saveOrEditData.accountEmailPwd"
                placeholder="请填写账号注册邮箱密码"
                maxlength="80"
                show-word-limit
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="注册国家" prop="accountCountry">
              <el-input
                v-model="saveOrEditData.accountCountry"
                placeholder="请填写账号注册国家"
                maxlength="80"
                show-word-limit
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="注册手机号" prop="accountPhoneNumber">
              <el-input
                v-model="saveOrEditData.accountPhoneNumber"
                placeholder="请填写账号注册手机号"
                maxlength="80"
                show-word-limit
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="账号类型" prop="webAccountTypeCode">
              <gd_param type="select"
                        paramCode="webAccountType"
                        placeholder="请选择账号类型"
                        @selectChange="accountTypeChange"
                        v-model="saveOrEditData.webAccountTypeCode" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="账号状态" prop="webAccountCode">
              <gd_param type="select"
                        paramCode="webAccountState"
                        placeholder="请选择账号状态"
                        @selectChange="accountStateChange"
                        v-model="saveOrEditData.webAccountCode" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="账号使用人" prop="locations">
              <el-input
                v-model="saveOrEditData.webStaffName"
                placeholder="请填写账号使用人，如果没有则不写"
                maxlength="80"
                show-word-limit
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="注册时间" prop="registerTime">
              <el-date-picker style="width: 100%"
                              v-model="saveOrEditData.registerTime"
                              type="date"
                              placeholder="请选择注册时间-精确到日"
                              value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="账号秘钥" prop="apiKey">
          <el-input
            type="textarea"
            placeholder="如有秘钥请填写"
            v-model="saveOrEditData.apiKey"
            maxlength="100"
            show-word-limit
          ></el-input>
        </el-form-item>

        <el-form-item label="备注" prop="comment">
          <el-input
            type="textarea"
            placeholder="如有备注请输入描述"
            v-model="saveOrEditData.comment"
            maxlength="100"
            show-word-limit
          ></el-input>
        </el-form-item>
        <!-- 按钮组 -->
        <div style="margin-top: 10px">
          <el-button type="primary" @click="submitSaveOrEdit" :loading="formLoading">确定</el-button>
          <el-button
            @click="
							dialog.saveOrEditDiv = false;
							resetAddOrEditOrShowForm();
						"
          >
            取消
          </el-button>
        </div>
      </el-form>
    </el-dialog>

    <!-- 详情弹窗 -->
    <el-dialog
      title="账号详情"
      :visible.sync="dialog.showDiv"
      width="85%"
      :show-close="false"
      :destroy-on-close="true"
      :close-on-click-modal="false"
    >
      <el-descriptions class="margin-top" :column="3" border>
        <el-descriptions-item label="账号">{{ showData.accountCode }}</el-descriptions-item>
        <el-descriptions-item label="密码">{{ showData.accountPwd }}</el-descriptions-item>
        <el-descriptions-item label="注册邮箱">{{ showData.accountEmail }}</el-descriptions-item>
        <el-descriptions-item label="邮箱密码">{{ showData.accountEmailPwd }}</el-descriptions-item>
        <el-descriptions-item label="注册手机">{{ showData.accountPhoneNumber }}</el-descriptions-item>
        <el-descriptions-item label="注册国家">{{ showData.accountCountry }}</el-descriptions-item>
        <el-descriptions-item label="注册时间">{{ showData.registerTime | timeFormat }}</el-descriptions-item>
        <el-descriptions-item label="账号类别">{{ showData.webAccountTypeName }}</el-descriptions-item>
        <el-descriptions-item label="账号状态">{{ showData.webAccountName }}</el-descriptions-item>
        <el-descriptions-item label="租赁-开始">{{ showData.leaseStartTime==null?'':(showData.leaseStartTime | timeFormat)  }}</el-descriptions-item>
        <el-descriptions-item label="租赁-结束">{{ showData.leaseStartTime==null?'':(showData.leaseEndTime | timeFormat) }}</el-descriptions-item>
        <el-descriptions-item label="账号所属人">{{ showData.webStaffName }}</el-descriptions-item>
        <el-descriptions-item label="秘钥（apiKey）" :span="3">{{ showData.apiKey }}</el-descriptions-item>
        <el-descriptions-item label="备注">{{ showData.comment }}</el-descriptions-item>
      </el-descriptions>
      <el-row v-if="showData.webAccountTypeCode=='1'">
        <el-divider content-position="left">ChatGPT-API-额度</el-divider>
        <el-descriptions class="margin-top" :column="4" border>
          <el-descriptions-item label="总额度">{{ showData.gptLimit==null?'暂未更新':showData.gptLimit }}</el-descriptions-item>
          <el-descriptions-item label="已使用">{{ showData.gptLimit==null?'暂未更新':showData.gptLimitUsed }}</el-descriptions-item>
          <el-descriptions-item label="剩余额度">{{ showData.gptLimit==null?'暂未更新':showData.gptLimitLess }}</el-descriptions-item>
          <el-descriptions-item label="有效期结束">{{ showData.gptLimit==null?'暂未更新':showData.gptPassTime }}</el-descriptions-item>
        </el-descriptions>
      </el-row>
      <el-button
        @click="
							dialog.showDiv = false;
							resetAddOrEditOrShowForm();
						"
      >
        关闭
      </el-button>
    </el-dialog>

    <!-- 租借账号弹窗 -->
    <el-dialog
      title="账号租借记录"
      :visible.sync="dialog.less"
      width="85%"
      :show-close="false"
      :destroy-on-close="true"
      :close-on-click-modal="false"
    >
      <el-form
        ref="saveOrEditDataForm"
        :rules="saveOrEditDataFormRules"
        :model="saveOrEditData"
        label-width="100px"
      >
        <el-row>
          <el-col :span="8">
            <el-form-item label="租赁客户" prop="webStaffName">
              <el-input
                v-model="saveOrEditData.webStaffName"
                placeholder="请填写租赁人"
                maxlength="80"
                show-word-limit
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="租赁开始时间" prop="leaseStartTime">
              <el-date-picker style="width: 100%"
                              v-model="saveOrEditData.leaseStartTime"
                              type="date"
                              placeholder="请选择租赁开始时间-精确到日"
                              value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="租赁结束时间" prop="leaseEndTime">
              <el-date-picker style="width: 100%"
                              v-model="saveOrEditData.leaseEndTime"
                              type="date"
                              placeholder="请选择租赁结束时间-精确到日"
                              value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <el-button type="primary" @click="saveLess" :loading="formLoading">确定租借</el-button>
      <el-button
        @click="
							dialog.less = false;
							resetAddOrEditOrShowForm();
						"
      >
        关闭
      </el-button>
    </el-dialog>

    <!-- 密码修改弹窗 -->
    <el-dialog
      title="密码修改记录"
      :visible.sync="dialog.changePwd"
      width="85%"
      :show-close="false"
      :destroy-on-close="true"
      :close-on-click-modal="false"
    >
      <el-form
        ref="saveOrEditDataForm"
        :rules="saveOrEditDataFormRules"
        :model="saveOrEditData"
        label-width="90px"
      >
        <el-form-item label="密码" prop="accountPwd">
          <el-input
            v-model="saveOrEditData.accountPwd"
            placeholder="请填写账号密码"
            maxlength="80"
            show-word-limit
          ></el-input>
        </el-form-item>
      </el-form>
      <el-button type="primary" @click="saveChangePwd" :loading="formLoading">确定记录修改</el-button>
      <el-button
        @click="
							dialog.changePwd = false;
							resetAddOrEditOrShowForm();
						"
      >
        关闭
      </el-button>
    </el-dialog>

    <!-- 记录账号售出弹窗 -->
    <el-dialog
      title="记录账号售出"
      :visible.sync="dialog.sale"
      width="85%"
      :show-close="false"
      :destroy-on-close="true"
      :close-on-click-modal="false"
    >
      <el-form
        ref="saveOrEditDataForm"
        :rules="saveOrEditDataFormRules"
        :model="saveOrEditData"
        label-width="90px"
      >
        <el-form-item label="售出客户" prop="webStaffName">
          <el-input
            v-model="saveOrEditData.webStaffName"
            placeholder="请填写售出客户"
            maxlength="80"
            show-word-limit
          ></el-input>
        </el-form-item>
      </el-form>
      <el-button type="primary" @click="saveSale" :loading="formLoading">确定售出</el-button>
      <el-button
        @click="
							dialog.sale = false;
							resetAddOrEditOrShowForm();
						"
      >
        关闭
      </el-button>
    </el-dialog>

    <!-- 记录弹窗 -->
    <el-dialog
      title="账号记录"
      :visible.sync="dialog.life"
      width="85%"
      :show-close="false"
      :destroy-on-close="true"
      :close-on-click-modal="false"
    >
      <el-timeline>
        <el-timeline-item v-for="(item,index) in accountInfoList" :key="index" :timestamp="item.infoTime | timeFormat" placement="top">
          <el-card>
            <p>{{ item.infoDesc}}</p>
          </el-card>
        </el-timeline-item>
      </el-timeline>
      <el-button
        @click="
							dialog.life = false;
							resetAddOrEditOrShowForm();
						"
      >
        关闭
      </el-button>
    </el-dialog>

  </div>
</template>

<script>
import * as axiosAPI from '@/api/gpt/webAccount.js';
import gd_param from "@/components/mycomponents/gd_param.vue";
import gd_file from "@/components/mycomponents/gd_file.vue";
import {updateAllGptLimit} from "@/api/gpt/webAccount.js";
import {apipath} from '@/util/apimain'
export default {
  name: 'webAccountIndex',
  components:{gd_file, gd_param},
  data() {
    return {
      //查询表单数据
      queryForm: {
        accountCode: null,
        accountEmail: null,
        accountCountry: null,
        accountPhoneNumber: null,
        webAccountCode: null,
        webAccountTypeCode: null,
        webStaffName: null
      },
      //表格数据
      tableData: [],
      //主表-分页数据
      pageData: {
        page: 1,
        size: 10,
        total: 0
      },
      //当前表格复选数据
      nowTableSelected: [],
      //弹窗状态
      dialog: {
        isEdit: false,
        saveOrEditDiv: false,
        showDiv: false,
        life: false,
        changePwd:false,
        less:false,
        sale:false
      },
      //添加编辑表单-检验
      saveOrEditDataFormRules:{
        accountCode: { required: true, message: '请输入账号id（登录使用账号名）', trigger: 'blur' },
        accountPwd: { required: true, message: '请输入该账号密码（保证和平台一致）', trigger: 'blur' },
        accountEmail: { required: true, message: '请输入账号注册邮箱', trigger: 'blur' },
        accountCountry: { required: true, message: '请输入账号注册国家', trigger: 'blur' },
        registerTime: { required: true, message: '请选择账号注册时间-精确到日', trigger: 'blur' },
        webAccountCode: { required: true, message: '请选择账号状态', trigger: 'change' },
        webAccountTypeCode: { required: true, message: '请选择账号类型', trigger: 'change' },
        accountEmailPwd: { required: true, message: '请输入注册邮箱密码', trigger: 'blur' }
      },
      saveOrEditData:{
        webAccountID:null,
        comment:null,
        accountCode:null,
        accountPwd:null,
        accountEmail:null,
        accountCountry:null,
        accountPhoneNumber:null,
        registerTime:null,
        webAccountCode:"1",
        webAccountName:"在库",
        webAccountTypeCode:null,
        webAccountTypeName:null,
        leaseStartTime:null,
        leaseEndTime:null,
        webStaffID:null,
        webStaffName:null,
        apiKey:null,
        accountEmailPwd:null,
        gptLimit:null,
        gptLimitUsed:null,
        gptLimitLess:null,
        gptPassTime:null
      },
      //展示表单数据
      showData: {
        webAccountID:null,
        comment:null,
        accountCode:null,
        accountPwd:null,
        accountEmail:null,
        accountCountry:null,
        accountPhoneNumber:null,
        registerTime:null,
        webAccountCode:null,
        webAccountName:null,
        webAccountTypeCode:null,
        webAccountTypeName:null,
        leaseStartTime:null,
        leaseEndTime:null,
        webStaffID:null,
        webStaffName:null,
        apiKey:null,
        accountEmailPwd:null,
        gptLimit:null,
        gptLimitUsed:null,
        gptLimitLess:null,
        gptPassTime:null
      },
      //表单加载值
      formLoading: false,
      // 账号记录
      accountInfoList:[]
    };
  },
  mounted() {
    this.getTableDatas();
  },
  filters:{
    timeFormat:function(value){
      let date = new Date(value);
      let year = date.getFullYear();
      let month = date.getMonth() + 1 +'';
      let day = date.getDate() +'';
      let hour = date.getHours() +'';
      let minute = date.getMinutes() +'';
      let second = date.getSeconds() +'';
      return `${year}-${month.padStart(2,'0')}-${day.padStart(2,'0')} ${hour.padStart(2,'0')}:${minute.padStart(2,'0')}:${second.padStart(2,'0')}`
    }
  },
  computed:{
    getTagType(){
      return(data) =>{
        if(data == "1"){
          return "success";
        }else if(data == "2"){
          return "info";
        }else if(data == "3"){
          return "warning";
        }else{
          return "ppp";
        }
      }
    }
  },
  methods: {

    //查询表格数据
    getTableDatas() {
      let _this = this;
      axiosAPI.pages(_this.queryForm, _this.pageData)
        .then(function(response) {
          console.log(response);
          if (response.code == '0000') {
            _this.tableData = response.data.rows;
            _this.pageData.total = response.data.total;
          }
        });
    },
    //重置查询条件表单
    resetQueryForm() {
      this.queryForm = {
        name: null,
        userName: null,
        warehouseName: null,
        recordTypeCount: null
      };
      this.getTableDatas();
    },
    //重置添加、编辑、展示表单
    resetAddOrEditOrShowForm() {
      this.showData = {
        webAccountID:null,
        comment:null,
        accountCode:null,
        accountPwd:null,
        accountEmail:null,
        accountCountry:null,
        accountPhoneNumber:null,
        registerTime:null,
        webAccountCode:null,
        webAccountName:null,
        webAccountTypeCode:null,
        webAccountTypeName:null,
        leaseStartTime:null,
        leaseEndTime:null,
        webStaffID:null,
        webStaffName:null,
        apiKey:null,
        accountEmailPwd:null,
        gptLimit:null,
        gptLimitUsed:null,
        gptLimitLess:null,
        gptPassTime:null
      };
      this.saveOrEditData = {
        webAccountID:null,
        comment:null,
        accountCode:null,
        accountPwd:null,
        accountEmail:null,
        accountCountry:null,
        accountPhoneNumber:null,
        registerTime:null,
        webAccountCode:"1",
        webAccountName:"在库",
        webAccountTypeCode:null,
        webAccountTypeName:null,
        leaseStartTime:null,
        leaseEndTime:null,
        webStaffID:null,
        webStaffName:null,
        apiKey:null,
        accountEmailPwd:null,
        gptLimit:null,
        gptLimitUsed:null,
        gptLimitLess:null,
        gptPassTime:null
      }
    },
    //提交-添加、编辑表单
    submitSaveOrEdit() {
      let _this = this;
      this.$refs.saveOrEditDataForm.validate(valid => {
        if (valid) {
          this.formLoading = true;
          let _this = this;
          if(_this.dialog.isEdit == true){// 编辑

            axiosAPI.update(_this.saveOrEditData)
              .then(function(res) {
                if (res.code == '0000') {
                  _this.dialog.saveOrEditDiv = false;
                  _this.resetAddOrEditOrShowForm();
                  _this.formLoading = false;
                  _this.getTableDatas();
                } else {
                  _this.formLoading = false;
                }
              });

          }else{ // 添加
            axiosAPI.add(_this.saveOrEditData)
              .then(function(res) {
                if (res.code == '0000') {
                  _this.dialog.saveOrEditDiv = false;
                  _this.resetAddOrEditOrShowForm();
                  _this.formLoading = false;
                  _this.getTableDatas();
                } else {
                  _this.formLoading = false;
                }
              });
          }
        }
      });
    },
    //提交-删除
    submitDelete(ids){
      let _this = this;
      axiosAPI.deletes(ids)
        .then(res => {
          if (res.code == '0000') {
            _this.getTableDatas();
          }
        });
    },
    //分页条目数切换
    tableSizeChange(size) {
      this.pageData.size = size;
      this.getTableDatas();
    },
    //页码切换
    tableCurrentChange(page) {
      this.pageData.page = page;
      this.getTableDatas();
    },
    //行-查看
    rowShow(data) {
      var _this = this;
      axiosAPI.getByID(data.webAccountID)
        .then(function(res){
          if(res.code == "0000"){
            _this.showData = res.data;
            _this.dialog.showDiv = true;
          }
        });
    },
    //行-编辑
    rowEdit(data) {

      let _this = this;
      axiosAPI.getByID(data.webAccountID)
        .then(function(res){
          if(res.code == "0000"){
            _this.saveOrEditData = res.data;
            _this.dialog.isEdit = true;
            _this.dialog.saveOrEditDiv = true;
          }
        });
    },
    //行-删除
    rowDelete(data) {
      this.$confirm('确定要删除这行数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.submitDelete(data.webAccountID);
      });
    },
    //点击批量删除
    moreDeletes() {
      if(this.nowTableSelected.length<=0){
        this.$notify.warning({
          title: '删除提示',
          message: '请至少选择一条数据再删除'
        });
        return;
      }
      let ids = '';

      for (var i in this.nowTableSelected) {
        ids += this.nowTableSelected[i].webAccountID + ',';
      }

      ids = ids.substring(0, ids.length - 1);

      //调用删除
      this.submitDelete(ids);
    },
    //表格批量选择
    tableDataSelected(data){
      this.nowTableSelected = data;
    },
    // 账号修改密码
    rowChangePwd(data){
      let _this = this;
      axiosAPI.getByID(data.webAccountID)
        .then(function(res){
          if(res.code == "0000"){
            _this.saveOrEditData = res.data;
            _this.dialog.changePwd = true;
          }
        });
    },
    // 保存账号密码修改
    saveChangePwd(){
      let _this = this;
      this.$refs.saveOrEditDataForm.validate(valid => {
        if (valid) {
          this.formLoading = true;
          axiosAPI.infoChangePwd(_this.saveOrEditData)
            .then(function(res) {
              if (res.code == '0000') {
                _this.dialog.changePwd = false;
                _this.resetAddOrEditOrShowForm();
                _this.formLoading = false;
                _this.getTableDatas();
              } else {
                _this.formLoading = false;
              }
            });
        }
      });
    },
    // 账号租借
    rowLess(data){
      let _this = this;
      axiosAPI.getByID(data.webAccountID)
        .then(function(res){
          if(res.code == "0000"){
            _this.saveOrEditData = res.data;
            _this.dialog.less = true;
          }
        });
    },
    // 保存账号租借信息
    saveLess(){
      let _this = this;
      this.$refs.saveOrEditDataForm.validate(valid => {
        if (valid) {
          this.formLoading = true;
          axiosAPI.lessAccount(_this.saveOrEditData)
            .then(function(res) {
              if (res.code == '0000') {
                _this.dialog.less = false;
                _this.resetAddOrEditOrShowForm();
                _this.formLoading = false;
                _this.getTableDatas();
              } else {
                _this.formLoading = false;
              }
            });
        }
      });
    },
    // 账号记录
    rowLife(data){
      let _this = this;
      axiosAPI.getAccountInfo(data.webAccountID)
        .then(function(res){
          if(res.code =="0000"){
            _this.accountInfoList = res.data;
            _this.dialog.life = true;
          }
        });
    },
    // 账号售出记录
    rowSaleAccount(data){
      let _this = this;
      axiosAPI.getByID(data.webAccountID)
        .then(function(res){
          if(res.code == "0000"){
            _this.saveOrEditData = res.data;
            _this.dialog.sale = true;
          }
        });
    },
    // 确定记录
    saveSale(){
      let _this = this;
      this.$refs.saveOrEditDataForm.validate(valid => {
        if (valid) {
          this.formLoading = true;
          axiosAPI.saleAccount(_this.saveOrEditData)
            .then(function(res) {
              if (res.code == '0000') {
                _this.dialog.sale = false;
                _this.resetAddOrEditOrShowForm();
                _this.formLoading = false;
                _this.getTableDatas();
              } else {
                _this.formLoading = false;
              }
            });
        }
      });
    },
    // 账号类型切换事件
    accountTypeChange(data){
      if(data == undefined){
        this.saveOrEditData.webAccountTypeCode = null;
        this.saveOrEditData.webAccountTypeName = null;
      }else{
        this.saveOrEditData.webAccountTypeCode = data.dataValue;
        this.saveOrEditData.webAccountTypeName = data.dataName;
      }
    },
    // 账号状态切换事件
    accountStateChange(data){
      if(data == undefined){
        this.saveOrEditData.webAccountCode = null;
        this.saveOrEditData.webAccountName = null;
      }else{
        this.saveOrEditData.webAccountCode = data.dataValue;
        this.saveOrEditData.webAccountName = data.dataName;
      }
    },
    // 日期格式化
    formatDate(date){
      const year = date.getFullYear();
      const month = (date.getMonth() + 1).toString().padStart(2, "0");
      const day = date.getDate().toString().padStart(2, "0");
      return `${year}-${month}-${day}`;
    },
    // 批量更新gpt额度
    async moreUpdateGPTLess(row){
      let _this = this;
      let msObj = this.$message({
        message: '批量更新中....请稍等....',
        type: 'warning',
        duration:0
      });
      axiosAPI.updateAllGptLimit()
        .then(function(res){
          if(res.code == "0000"){
            msObj.close();
            _this.getTableDatas();
          }
        });
    },
    // 查询GPT额度
    async rowGPTLimit(row) {
      if(row.apiKey==null||row.apiKey==""||row.apiKey==undefined){
        this.$notify.warning({
          title: '操作提示',
          message: '该账号无密钥无法查询'
        });
        return false;
      }
      let _this = this;
      axiosAPI.getByID(row.webAccountID)
        .then(function(res){
          if(res.code == "0000"){
            let tempData = res.data;
            axiosAPI.updateGptLimit(tempData)
              .then(function(res){
                if(res.code == "0000"){
                  _this.getTableDatas();
                }
              });
          }
        });

    }

  }
};
</script>

<style scoped></style>
